<?php
/**
 * 需要进行登陆和权限检查的控制器基类
 */
class Controller_NeedLogin extends Controller_Base {
	// 分页每页显示条数
	protected $page_size = 15;
	/**
	 * 分发操作前，检查登陆和权限
	 * @return [type] [description]
	 */
	public function preDispatch() {
		$this->checkLogin();
		$this->checkPriv();
	}

	/**
	 * 检查登陆
	 * @return [type] [description]
	 */
	public function checkLogin() {
		isset($this->_session->user_name) || $this->_ssoLogin();
	}

	/**
	 * 检查权限
	 * @return [type] [description]
	 */
	public function checkPriv() {
		if ($this->_session->is_admin) {
			return true;
		}
		$model = new Application_Model_Privilege();
		// $auth = $model->getUserPrivilege($this->_session->user_id);
		$auth = $this->_session->currentLoginUser['auth'];

		$menu_id = $model->getMenuId($this->module, $this->controller, $this->action);
		// http请求类
		$http_request = new Zend_Controller_Request_Http();
		if ($menu_id) {
			if (!in_array($menu_id, $auth)) {
				$msg = "您没有权限";
				if ($http_request->isXmlHttpRequest()){
					exit(json_encode(array('status'=>false,'success'=>false,'opr'=>false,'content'=>$msg,'msg'=>$msg,'message'=>$msg)));
				} else {
					$this->showError("您没有权限");
				}
			}
		} else {
			// 默认未添加的菜单是否开放访问（）
			$need_check = Helper_Config::get('base', 'access', 'check');
			if ($need_check) {
				$msg = "该操作未加入权限控制";
				if ($http_request->isXmlHttpRequest()){
					exit(json_encode(array('status'=>false,'content'=>$msg,'msg'=>$msg,'message'=>$msg)));
				} else {
					$this->showError($msg);
				}
			}
		}
	}

	/**
	 * 返回当前登录用户信息
	 * @return array
	 */
	public function currentLoginUser($key = '') {

		if(empty($key))
			return $this->_session->currentLoginUser;
		return $this -> _session -> currentLoginUser[$key];
	}

	/**
	 * 分页类
	 * @param number $total_num
	 * @param number $page_size
	 * @param string $action
	 * @param number $mode
	 * @return string
	 */
	public function page($total_num = 0, $page_size = 30, $action = '', $mode = 1) {
		$filter = array();
		$getQuery = $this->getRequest()->getQuery();
		$filter['record_count'] = $total_num;
		if (isset($getQuery['page_size']) && intval($getQuery['page_size']) > 0) {
			$filter['page_size'] = intval($_GET['page_size']);
		} else {
			$filter['page_size'] = $page_size;
		}

		$filter['page'] = (empty($getQuery['page']) || intval($getQuery['page']) <= 0) ? 1 : intval($getQuery['page']);

		$filter['page_count'] = (!empty($filter['record_count']) && $filter['record_count'] > 0) ? ceil($filter['record_count'] / $filter['page_size']) : 1;
		if ($filter['page'] > $filter['page_count']) {
			$filter['page'] = $filter['page_count'];
		}

		$pagestr = '';
		if ($action != '') {
			$data = array(
				'total' => $filter['record_count'],
				'perpage' => $filter['page_size'],
				'ajax' => $action,
			);
		} else {
			$data = array(
				'total' => $filter['record_count'],
				'perpage' => $filter['page_size'],
			);
		}
		if ($filter['record_count'] > 0) {
			$page = new Page($data);
			$pagestr = $page->show($mode);
		}
		return $pagestr;
	}
}